FROM debian:bullseye

LABEL version="1.1"
LABEL author="kamon<350665550@qq.com>"
LABEL description="FreeSwitch For Debian"

# timezone
ENV LANG C.UTF-8
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone

# dns & hostname
RUN echo "nameserver 8.8.8.8 \
nameserver 114.114.114.114" > /etc/resolv.conf
RUN echo "freeswitch" > /etc/hostname
RUN echo "127.0.0.1 freeswitch" >> /etc/hosts

# source & updata & vpn
ADD conf/sources.list /etc/apt/sources.list
RUN apt-get update && apt-get upgrade -y
RUN apt-get install libterm-readkey-perl -y

#workdir
#WORKDIR /usr/src

# install
ENV TOKEN=pat_qm19biDa86nMJMpKeW7dX4qP
RUN apt-get update && apt-get install -y gnupg2 wget lsb-release
RUN wget --http-user=kamon --http-password=$TOKEN -O /usr/share/keyrings/signalwire-freeswitch-repo.gpg https://freeswitch.signalwire.com/repo/deb/debian-release/signalwire-freeswitch-repo.gpg
RUN echo "machine freeswitch.signalwire.com login signalwire password $TOKEN" > /etc/apt/auth.conf
RUN echo "deb [signed-by=/usr/share/keyrings/signalwire-freeswitch-repo.gpg] https://freeswitch.signalwire.com/repo/deb/debian-release/ `lsb_release -sc` main" > /etc/apt/sources.list.d/freeswitch.list
RUN echo "deb-src [signed-by=/usr/share/keyrings/signalwire-freeswitch-repo.gpg] https://freeswitch.signalwire.com/repo/deb/debian-release/ `lsb_release -sc` main" >> /etc/apt/sources.list.d/freeswitch.list
RUN apt-get update && apt-get install -y freeswitch-meta-all

# modules & config & sounds
ADD conf/lang/zh /etc/freeswitch/lang/zh
ADD conf/sounds/zh /usr/share/freeswitch/sounds/zh
RUN sed -i "s/<\!-- <load module=\"mod_say_zh\"\/> -->/<load module=\"mod_say_zh\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i "s/<\!-- <load module=\"mod_python3\"\/> -->/<load module=\"mod_python3\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i "s/<\!--.*<load module=\"mod_sms\"\/>.*-->/<load module=\"mod_sms\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i "s/<\!--.*<load module=\"mod_curl\"\/>.*-->/<load module=\"mod_curl\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i "s/<\!--.*<load module=\"mod_h26x\"\/>.*-->/<load module=\"mod_h26x\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i "s/<\!--.*<load module=\"mod_shout\"\/>.*-->/<load module=\"mod_shout\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i "s/<\!--.*<load module=\"mod_callcenter\"\/>.*-->/<load module=\"mod_callcenter\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
#RUN sed -i "s/<\!--.*<load module=\"mod_translate\"\/>.*-->/<load module=\"mod_translate\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i "s/<\!--.*<load module=\"mod_mariadb\"\/>.*-->/<load module=\"mod_mariadb\"\/>/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i "s/<load module=\"mod_signalwire\"\/>/<\!-- <load module=\"mod_signalwire\"\/> -->/" /etc/freeswitch/autoload_configs/modules.conf.xml
RUN sed -i -e "s/data=\"default_password=.*\?\"/data=\"default_password=123456\"/" /etc/freeswitch/vars.xml
RUN sed -i -e "s/data=\"sound_prefix=.*\?\"/data=\"sound_prefix=\$\${sounds_dir}\/zh\/cn\/sinmei\"/" /etc/freeswitch/vars.xml
RUN sed -i -e "s/data=\"global_codec_prefs=.*\?\"/data=\"global_codec_prefs=PCMU,PCMA,G722,G729,OPUS,H264,VP8\"/" /etc/freeswitch/vars.xml
RUN sed -i -e "s/data=\"outbound_codec_prefs=.*\?\"/data=\"outbound_codec_prefs=PCMU,PCMA,G722,G729,OPUS,H264,VP8\"/" /etc/freeswitch/vars.xml
RUN sed -i -e "s/data=\"default_country=.*\?\"/data=\"default_country=CN\"/" /etc/freeswitch/vars.xml
RUN sed -i -e "s/data=\"default_areacode=.*\?\"/data=\"default_areacode=208\"/" /etc/freeswitch/vars.xml
RUN sed -i "s/<X-PRE-PROCESS cmd=\"set\" data=\"default_country=CN\"\/>/<X-PRE-PROCESS cmd=\"set\" data=\"default_country=CN\"\/>\n  <X-PRE-PROCESS cmd=\"set\" data=\"default_language=zh\"\/>/" /etc/freeswitch/vars.xml
RUN sed -i "s/<X-PRE-PROCESS cmd=\"set\" data=\"default_language=zh\"\/>/<X-PRE-PROCESS cmd=\"set\" data=\"default_language=zh\"\/>\n  <X-PRE-PROCESS cmd=\"set\" data=\"default_voice=sinmei\"\/>/" /etc/freeswitch/vars.xml
RUN sed -i "s/<\/include>/  <X-PRE-PROCESS cmd=\"set\" data=\"timezone=Asia\/Shanghai\"\/>\n<\/include>/" /etc/freeswitch/vars.xml
RUN sed -i -e "s/data=\"internal_ssl_enable=false\"/data=\"internal_ssl_enable=true\"/" /etc/freeswitch/vars.xml
RUN sed -i -e "s/data=\"external_ssl_enable=false\"/data=\"external_ssl_enable=true\"/" /etc/freeswitch/vars.xml
RUN sed -i "s/<section name=\"languages\" description=\"Language Management\">/<section name=\"languages\" description=\"Language Management\">\n    <X-PRE-PROCESS cmd=\"include\" data=\"lang\/zh\/zh.xml\"\/>/" /etc/freeswitch/freeswitch.xml
RUN sed -i "s/<param name=\"listen-ip\" value=\"::\"\/>/<param name=\"listen-ip\" value=\"127\.0\.0\.1\"\/>/" /etc/freeswitch/autoload_configs/event_socket.conf.xml
RUN sed -i -e "s/<\!--.*<param name=\"enable-timer\" value=\"false\"\/>.*-->/<param name=\"enable-timer\" value=\"false\"\/>/" /etc/freeswitch/sip_profiles/internal.xml
RUN sed -i -e "s/<param name=\"apply-nat-acl\" value=\"nat.auto\"\/>/<param name=\"apply-nat-acl\" value=\"nat.auto\"\/>\n\n    <param name=\"apply-candidate-acl\" value=\"wan\"\/>\n    <param name=\"apply-candidate-acl\" value=\"localnet.auto\"\/>\n    <param name=\"apply-candidate-acl\" value=\"rfc1918.auto\"\/>/" /etc/freeswitch/sip_profiles/internal.xml
RUN sed -i "s/<param name=\"local-network-acl\" value=\"localnet.auto\"\/>/<param name=\"local-network-acl\" value=\"rfc1918.auto\"\/>/" /etc/freeswitch/sip_profiles/internal.xml
RUN sed -i "s/<param name=\"local-network-acl\" value=\"localnet.auto\"\/>/<param name=\"local-network-acl\" value=\"rfc1918.auto\"\/>/" /etc/freeswitch/sip_profiles/external.xml
RUN sed -i -e "s/name=\"rtp-timeout-sec\"/name=\"media_timeout\"/" /etc/freeswitch/sip_profiles/internal.xml
RUN sed -i -e "s/name=\"rtp-hold-timeout-sec\"/name=\"media_hold_timeout\"/" /etc/freeswitch/sip_profiles/internal.xml
RUN sed -i -e "s/name=\"rtp-timeout-sec\"/name=\"media_timeout\"/" /etc/freeswitch/sip_profiles/external.xml
RUN sed -i -e "s/name=\"rtp-hold-timeout-sec\"/name=\"media_hold_timeout\"/" /etc/freeswitch/sip_profiles/external.xml
RUN mv /etc/freeswitch/sip_profiles/external-ipv6.xml /etc/freeswitch/sip_profiles/external-ipv6.xml.noload
RUN mv /etc/freeswitch/sip_profiles/internal-ipv6.xml /etc/freeswitch/sip_profiles/internal-ipv6.xml.noload
RUN mv /etc/freeswitch/dialplan/default/01_example.com.xml /etc/freeswitch/dialplan/default/01_example.com.xml.noload
RUN mv /etc/freeswitch/directory/default/default.xml /etc/freeswitch/directory/default/default.xml.noload
RUN mv /etc/freeswitch/directory/default/example.com.xml /etc/freeswitch/directory/default/example.com.xml.noload
RUN sed -i -e "s/<param name=\"max-sessions\" value=\".*\?\"\/>/<param name=\"max-sessions\" value=\"3000\"\/>/" /etc/freeswitch/autoload_configs/switch.conf.xml
RUN sed -i -e "s/<param name=\"sessions-per-second\" value=\".*\?\"\/>/<param name=\"sessions-per-second\" value=\"90\"\/>/" /etc/freeswitch/autoload_configs/switch.conf.xml
RUN echo "<include>\n\
    <extension name=\"telephone\">\n\
        <condition field=\"destination_number\" expression=\"^(\d{5,15})$\">\n\
            <action application=\"set\" data=\"effective_caller_id_number=\${outbound_caller_id_number}\"/>\n\
            <action application=\"set\" data=\"effective_caller_id_name=\${outbound_caller_id_name}\"/>\n\
            <action application=\"bridge\" data=\"sofia/gateway/gw/\$1\"/>\n\
        </condition>\n\
    </extension>\n\
</include>" > /etc/freeswitch/dialplan/default/telephone.xml
RUN echo "<include>\n\
    <gateway name=\"gw\">\n\
        <!--<param name=\"username\" value=\"1000\"/>-->\n\
        <!--<param name=\"password\" value=\"123456\"/>-->\n\
        <!--<param name=\"realm\" value=\"192.168.1.11\"/>-->\n\
        <!--<param name=\"proxy\" value=\"192.168.1.11:5060\"/>-->\n\
        <!--<param name=\"register\" value=\"true\"/>-->\n\
    </gateway>\n\
</include>" > /etc/freeswitch/sip_profiles/external/gw.xml
RUN chown -R freeswitch:freeswitch /etc/freeswitch
RUN chown -R freeswitch:freeswitch /usr/share/freeswitch/sounds

#system config
RUN apt-get install vim -y
ADD conf/vimrc.local /etc/vim/vimrc.local
RUN echo "alias ll='ls -l --color'" >> /root/.bashrc
RUN echo "alias grep='grep --color'" >> /root/.bashrc
RUN echo "*   soft   nofile   65535" >> /etc/security/limits.conf
RUN echo "*   hard   nofile   65535" >> /etc/security/limits.conf

# Volumes
#VOLUME ["/etc/freeswitch"]
#VOLUME ["/tmp"]

# clean
RUN apt-get autoremove -y && rm -rf /var/lib/apt/lists/*

# entrypoint
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT /entrypoint.sh && tail -f /dev/null